<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-row>
        <el-col :span="8" style="text-align: left">
          <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary" v-waves
                     :loading="downloadLoading" icon="el-icon-download" @click="exportExcel">导出
          </el-button>
        </el-col>
        <el-col :span="16" style="text-align: right">
          <el-input size="small" style="width: 200px" class="filter-item fsc-search" placeholder="订单号-回车查询" v-model="listQuery.condition.orderNo" @keyup.enter.native="searchClick"></el-input>
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-search" @click="searchClick">
            搜索
          </el-button>
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-circle-plus-outline" @click="moreClick">更多
          </el-button>
          <el-dropdown trigger="click" :hide-on-click=false style="float: right;margin-left: 10px">
            <span class="el-dropdown-link">
              <el-button class="filter-item" size="small" type="primary" v-waves>
                选择列
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-checkbox-group v-model="theadCheckVal">
                  <el-checkbox label="ownerOrderNo">订单号</el-checkbox>
                  <el-checkbox label="storeHouseName">收货仓库</el-checkbox>
                  <el-checkbox label="ownerId">客户</el-checkbox>
                  <el-checkbox label="carrierName">承运商</el-checkbox>
                  <el-checkbox label="materielId">车型</el-checkbox>
                  <el-checkbox label="lotNo1">车架号</el-checkbox>
                  <el-checkbox label="taskNode">异常分类</el-checkbox>
                  <el-checkbox label="status">质检结果</el-checkbox>
                  <el-checkbox label="userCreate">质检员</el-checkbox>
                  <el-checkbox label="gmtCreate">质检时间</el-checkbox>
                  <el-checkbox label="exDescStr">异常描述</el-checkbox>
                  <el-checkbox label="missDescStr">缺件信息</el-checkbox>
                  <el-checkbox label="picKey">异常图片</el-checkbox>
                </el-checkbox-group>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
      <div style="clean:both"></div>
    </div>

    <div v-show="searchFlag"  style="height: auto;margin-top: 10px;">
      <el-form ref="form" :model="listQuery.condition" label-width="80px" :inline="true">
        <el-form-item label=客户:>
          <el-input  placeholder="请输入客户" class="w200" clearable v-model="listQuery.condition.ownerId" ></el-input>
        </el-form-item>
        <el-form-item label=车架号:>
          <!--<el-input  placeholder="请输入订单号" class="w200" clearable v-model="listQuery.condition.orderNo" ></el-input>-->
          <el-input type="textarea" :rows="2" style="width: 250px"
                    v-model="listQuery.condition.lotNo1"  placeholder="请输入车架号, 以逗号来分隔"></el-input>
        </el-form-item>
        <el-form-item label="承运商:">
          <el-input  placeholder="请输入承运商" class="w200" clearable v-model="listQuery.condition.carrierName" ></el-input>
        </el-form-item>
        <el-form-item label="质检时间:">
          <el-date-picker class="w200" v-model="listQuery.condition.startDate" type="date" placeholder="选择日期时间"  value-format="yyyy-MM-dd"
                          align="right" :picker-options="timePicker"></el-date-picker>
          <span class="zhi-font">至</span>
          <el-date-picker class="w200" v-model="listQuery.condition.endDate" type="date" placeholder="选择日期时间"  value-format="yyyy-MM-dd"
                          align="right" :picker-options="timePicker"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchClick" style="margin-left: 10px">查询</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div style="clean:both"></div>

    <!--列表信息-->
    <el-table ref="expectionTable" :data="exceptionList" v-loading="listLoading" element-loading-text="加载中" border fit highlight-current-row style="width: 100%;margin-top:5px">
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column :key="key" align="center" :width="value.width" v-for='(value,key) in tableThead' :label="value.title">
        <template slot-scope="scope">
          <div v-if="value.key === 'taskNode'">{{scope.row[value.key] | exceptionTaskNodeFilter}}</div>
          <div v-else-if="value.key === 'status'">{{scope.row[value.key] | inboundInspectStatusFilter}}</div>
          <!--异常描述-->
          <div v-else-if="value.key === 'exDescStr'">
            <div class="detailBox">
              <div v-for="(item,index) in scope.row.ex_arr">{{item}}</div>
            </div>
          </div>
          <!--缺件信息-->
          <div v-else-if="value.key === 'missDescStr'">
            <div class="detailBox">
              <div v-for="(item,index) in scope.row.miss_arr">{{item}}</div>
            </div>
          </div>
          <!--查看图片-->
          <div v-else-if="value.key === 'picKey'">
            <el-button type="primary" @click="checkPic(scope.row.picInfo)">查看</el-button>
          </div>
          <div v-else>{{scope.row[value.key]}}</div>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.current" :page-sizes="[10,20,30,50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="listQuery.total">
      </el-pagination>
    </div>
    <el-dialog title="异常图片" :visible.sync="dialogVisible" :width=" picMaxLenght * 345+ 'px'" >
      <hr>
      <el-card class="box-card" v-for="(item,index) in imgList" :key="item.id">
        <div slot="header" class="clearfix">
          <span>{{item.exDescribe}}</span>
        </div>
        <div style="margin: 10px auto">
          <img v-for="(pic,index) in item.pictures" :src="pic.picUrl" alt="图片丢失" style="width: 300px;margin:0 6px" >
        </div>
      </el-card>
    </el-dialog>
  </div>
</template>
<script>
  import * as inspectAPI from '@/api/inbound/inboundInspect'
  import waves from '@/directive/waves' // 水波纹指令
  import * as Utils from '@/utils/index'
  const defautlTableThead = [
    { key: 'ownerOrderNo', title: '订单号', width: 200 },
    { key: 'storeHouseName', title: '收货仓库', width: 180 },
    { key: 'ownerId', title: '客户', width: 180 },
    { key: 'carrierName', title: '承运商', width: 180 },
    { key: 'materielId', title: '车型', width: 180 },
    { key: 'lotNo1', title: '车架号', width: 180 },
    { key: 'taskNode', title: '异常分类', width: '' },
    { key: 'status', title: '质检结果', width: '' },
    { key: 'userCreate', title: '质检员', width: '' },
    { key: 'gmtCreate', title: '质检时间', width: 180 },
    { key: 'exDescStr', title: '异常描述', width: 180 },
    { key: 'missDescStr', title: '缺件信息', width: 180 },
    { key: 'picKey', title: '异常图片', width: '' }
  ]
  export default {
    name: 'exception',
    directives: {
      waves
    },
    data() {
      return {
        theadCheckVal: [
          'ownerOrderNo',
          'storeHouseName',
          'ownerId',
          'carrierName',
          'materielId',
          'lotNo1',
          'taskNode',
          'status',
          'userCreate',
          'gmtCreate',
          'exDescStr',
          'missDescStr',
          'picKey'
        ],
        tableThead: defautlTableThead,
        listQuery: {
          current: 1,
          size: 10,
          total: 0,
          condition: {
            startDate: Utils.parseTime(new Date().getTime(), '{y}-{m}-{d} 00:00:00'),
            endDate: Utils.parseTime(new Date().getTime(), '{y}-{m}-{d} 23:59:59')
          }
        },
        exDescribeList: [],
        imgList: [],
        exceptionList: [],
        listLoading: true,
        searchFlag: true,
        dialogVisible: false,
        downloadLoading: false,
        downloadData: [],
        picMaxLenght: 1,
        timePicker: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        }
      }
    },
    created() {
      this.list()
    },
    watch: {
      theadCheckVal(valArr) {
        this.tableThead = defautlTableThead.filter(v => {
          for (var i = 0; i < valArr.length; i++) {
            if (v.key === valArr[i]) {
              return v
            }
          }
        })
      }
    },
    methods: {
      list() {
        // 列表查询
        this.listLoading = true
        // 处理是否展开更多
        let queryParam = {}
        queryParam = this.listQuery
        queryParam.condition.houseId = this.$store.getters.currentStorehouse.id
        inspectAPI.listInpsect(queryParam).then(result => {
          this.listLoading = false
          if (result.code === 0) {
            this.exceptionList = result.data.records
            this.listQuery.total = result.data.total
          } else {
            this.$message({
              title: '信息提示',
              message: result.message,
              type: 'error',
              duration: 2000
            })
          }
        }).catch(() => {
          this.listLoading = false
          // this.reset()
        })
      },
      changeStatus() {
        return this.downloadData.map(v => {
          if (v.status === '0') {
            v.status = '未质检'
          } else if (v.status === '10') {
            v.status = '全部合格'
          } else if (v.status === '20') {
            v.status = '部分合格'
          } else if (v.status === '30') {
            v.status = '全部破损'
          }
          if (v.taskNode === '0') {
            v.taskNode = '指令'
          } else if (v.taskNode === '10') {
            v.taskNode = '寻车'
          } else if (v.taskNode === '20') {
            v.taskNode = '移车'
          } else if (v.taskNode === '30') {
            v.taskNode = '提车'
          } else if (v.taskNode === '40') {
            v.taskNode = '收车质检'
          } else if (v.taskNode === '41') {
            v.taskNode = '入库移车'
          } else if (v.taskNode === '42') {
            v.taskNode = '分配入库'
          } else if (v.taskNode === '50') {
            v.taskNode = '出库备车'
          } else if (v.taskNode === '51') {
            v.taskNode = '出库确认'
          } else if (v.taskNode === '60') {
            v.taskNode = '装车交验'
          }
          return v
        })
      },
      exportExcel() {
        // 数据导出
        let queryParam = {}
        queryParam = this.$data.listQuery.condition
        queryParam.houseId = this.$store.getters.currentStorehouse.id
        this.downloadLoading = true
        inspectAPI.exportExcpList(queryParam).then(result => {
          this.downloadLoading = false
          if (result.code === 0) {
            this.downloadData = result.data
            if (this.downloadData.length) {
              const arr = this.changeStatus()
              require.ensure([], () => {
                const { export_json_to_excel } = require('@/vendor/Export2Excel')
                const tHeader = ['订单号', '车型', '承运商', '车架号', '质检结果', '质检员', '质检时间', '异常分类', '异常描述', '缺件信息']
                const filterVal = ['ownerOrderNo', 'materielId', 'carrierName', 'lotNo1', 'status', 'userCreate', 'gmtCreate', 'taskNode', 'exDescStr', 'missDescStr']
                const data = this.formatJson(filterVal, arr)
                export_json_to_excel(tHeader, data, this.filename)
              })
            }
          } else {
            this.$notify({
              title: '信息提示',
              message: result.message,
              type: 'error',
              duration: 2000
            })
          }
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      },
      handleSizeChange(val) {
        // 分页size变化
        this.$data.listQuery.size = val
        this.list()
      },
      handleCurrentChange(val) {
        // 分页当前页变化
        this.$data.listQuery.current = val
        this.list()
      },
      moreClick() {
        // 更多click
        this.$data.searchFlag = !this.$data.searchFlag
      },
      searchClick() {
        // 搜索
        this.list()
      },
      reset() {
        this.$data.listQuery = {
          current: 1,
          size: 10,
          total: 0,
          condition: {}
        }
        this.list()
      },
      checkPic(value) {
        const that = this
        that.picMaxLenght = 1
        this.dialogVisible = true
        this.imgList = value
        if (this.imgList && this.imgList.length >= 1) {
          this.imgList.map(i => {
            if (i.pictures.length > that.picMaxLenght) {
              that.picMaxLenght = i.pictures.length
            }
          })
        }
      }
    }
  }
</script>
<style type="text/scss" lang="scss" scoped>
  .detailBox{
    height: 22px;
    overflow: hidden;
    cursor: pointer;
    &:hover{
      overflow: visible!important;
      height: auto!important;
    }
  }
  .box-card {
    margin: 10px 10px 0 0;
  }
</style>
